﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Microsoft.Win32;
using System.Windows;
using System.Xml;
using System.Windows.Media;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 以此类为模板，制作自定义命令类
    ///     共需要修改１０处！！！
    /// ==============================
    /// 
    /// 插入备注文本块命令。
    /// ★①，修改类名。
    /// </summary>
    public static class InsertFixedTitleCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        /// <summary>
        /// ★②，修改静态构造方法名。
        /// </summary>
        static InsertFixedTitleCommand()//类型构造器
        {
            //★③，修改两个字符串参数名。★④以及第三个参数的类型名。
            routedUICmd = new RoutedUICommand(
                "InsertFixedTitleCommand",
                "InsertFixedTitleCommand",
                typeof(InsertFixedTitleCommand),//创建RoutedUICommand对象
                null);//插入备注文本块不给快捷键组合。

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            //★⑦，修改此方法的实现。

            //如何插入一个图片？？？

            AddFixedTitle();
        }

        public static void AddFixedTitle(Point? point = null)
        {
            Title mainSelectedTitle = null;

            if (Globals.MainWindow.SelectedTitlesList.Count <= 0)
            {
                if (Globals.MainWindow.SelectedTitleImage == null) return;

                mainSelectedTitle = Globals.MainWindow.SelectedTitleImage.MasterTitle;
            }

            foreach (Title t in Globals.SelectedTitlesList)
            {
                if (t.IsMainSelectedTitle)
                {
                    mainSelectedTitle = t; break;
                }
            }

            if (mainSelectedTitle == null) return;
            if (mainSelectedTitle.IsFixed) return;

            Point selCenter = new Point(mainSelectedTitle.LeftTop.X + mainSelectedTitle.ActualWidth / 2,
                mainSelectedTitle.LeftTop.Y + mainSelectedTitle.ActualHeight / 2);

            FixedTitle ft = new FixedTitle(Globals.MainWindow, mainSelectedTitle);

            XmlNode titleSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.SelectSingleNode("TitleSet");
            if (titleSetNode == null)
            {
                titleSetNode = Globals.MainWindow.MainXmlDocument.DocumentElement.AppendXml("<TitleSet />");
            }

            XmlNode newData;

            if (point != null)
            {
                Point pt = (Point)point;

                Point newLocation;

                if (ft.Editor.RootTitle == ft.GetHigherLvelTitle())
                {
                    newLocation = pt;
                }
                else
                {
                    newLocation = new Point(pt.X - selCenter.X, pt.Y - selCenter.Y);
                }

                ft.Location = newLocation;

                newData = titleSetNode.AppendXml("<Title IsFixed=\"True\" HigherLevelTitleID=\"" +
                    mainSelectedTitle.ID + "\" Location=\"" + newLocation.ToString() +
                    "\"><TextSet><Text FontSize=\"15\">..自适应标题..</Text></TextSet></Title>");

            }
            else
            {
                newData = titleSetNode.AppendXml("<Title IsFixed=\"True\" HigherLevelTitleID=\"" + mainSelectedTitle.ID +
                    "\" ><TextSet><Text FontSize=\"15\">..自适应标题..</Text></TextSet></Title>");
            }

            ft.XmlData = newData;
            ft.RebuildANewID();

            Globals.MainWindow.mainCanvas.Children.Add(ft);

            ModifingItem mi = new ModifingItem()
            {
                IsNeedRefreshRelations = true,
                ModifingName = "添加自适应标题"
            };

            Action actAddFixedTitle = new Action(ft.ID, -1, ft.GetType().Name, "AddFixedTitle", null,
                ft.XmlData.OuterXml, ActionType.AddFixedTitle, ft.HigherLevelTitleID);

            mi.Add(actAddFixedTitle);

            ft.RefreshLoaction();

            ft.SelectOnlySelf();
            ft.RefreshEditorTitleDraggingHandler();

            Globals.MainWindow.Do(mi);
            Globals.MainWindow.IsModified = true;
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            //★⑧，修改此方法的实现。

            if (Globals.SelectedTitlesList.Count != 1)
            {
                e.CanExecute = false; return;
            }

            Title t = Globals.SelectedTitlesList[0];
            if (t.IsFixed)
            {
                e.CanExecute = false; return;
            }
            else
            {
                e.CanExecute = true;
            }
        }
    }
}
